package com.jzo2o.foundations.controller.operation;


import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 前端控制器
 * </p>
 *
 * @author itcast
 * @since 2023-07-03
 */
@Validated
@RestController("ServerController")
@RequestMapping("/operation/serve")
@Api(tags = "运营端 - 服务区域相关接口")
@Slf4j
public class ServerController {
    @Resource
    private IServerService serverService;

    @GetMapping("/page")
    @ApiOperation("服务区域分页")
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO reqDTO) {
        log.info("reqDTO={}", reqDTO);
        return serverService.page(reqDTO);
    }

    @PostMapping("/batch")
    @ApiOperation("区域服务批量新增")
    public void add(@RequestBody List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        serverService.addBatch(serveUpsertReqDTOList);
    }

    @PutMapping("/{id}")
    @ApiOperation("区域服务价格修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "price", value = "价格", required = true, dataTypeClass = BigDecimal.class)
    })
    public void update( @PathVariable("id") Long id,
                        @RequestParam("price") BigDecimal price) {
        serverService.lambdaUpdate().setSql("price = " + price)
                .eq(Serve::getId, id)
                .update();
    }

    @PutMapping("/onSale/{id}")
    @ApiOperation("区域服务上架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
    })
    public void onSale(@PathVariable("id") Long id) {
        serverService.onSale(id);

    }

    @DeleteMapping("/{id}")
    @ApiOperation("区域服务删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
    })
    public void delete(@PathVariable("id") Long id) {
        serverService.removeById(id);
    }

    @PutMapping("/offSale/{id}")
    @ApiOperation("区域服务下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
    })
    public void offSale(@PathVariable("id") Long id) {
        serverService.lambdaUpdate()
                .set(Serve::getSaleStatus, 1)
                .eq(Serve::getId, id)
                .update();
    }

    @PutMapping("/onHot/{id}")
    @ApiOperation("设置热门服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
    })
    public void onHot(@PathVariable("id") Long id) {
        serverService.lambdaUpdate()
                .set(Serve::getIsHot, 1)
                .set(Serve::getHotTimeStamp, System.currentTimeMillis())
                .eq(Serve::getId, id)
                .update();
    }

    @PutMapping("/offHot/{id}")
    @ApiOperation("取消热门服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "服务id", required = true, dataTypeClass = Long.class),
    })
    public void offHot(@PathVariable("id") Long id) {
        serverService.lambdaUpdate()
                .set(Serve::getIsHot, 0)
                .eq(Serve::getId, id)
                .update();
    }

}
